<?php



class PHPThread {
	var $pref ; // process reference
	var $pipes; // stdio
	var $buffer; // output buffer
	/* private */ function PHPThread() {
	$this->pref = 0;
	$this->stdout  = "";
	$this->pipes = (array)NULL;
	}
	/* public static */ function Create ($file) {
	$t = new PHPThread;
	$descriptor = array (0 => array ("pipe", "r"), 1 => array ("pipe", "w"), 2 => array ("pipe", "w"));
	$proc_options = array('bypass_shell' => "1");

	$t->pref = proc_open ("start c:/xAP/apache/bin/php -c c:/xAP/apache/bin/ -q $file ", $descriptor, $t->pipes);
	stream_set_blocking ($t->pipes[1], 0);
	stream_set_blocking ($t->pipes[2], 0);
	usleep (10);
	return $t;
	}
	/* public instance */ function isActive () {
	$this->buffer .= $this->listen();
	//echo $this->buffer;
	$f = stream_get_meta_data ($this->pipes[1]);
	return !$f["eof"];
	}
	/* public instance */ function close () {
	$r = proc_close ($this->pref);
	$this->pref = NULL;
	return $r;
	}
	/* public instance */ function tell ($thought) {
	fwrite ($this->pipes[0], $thought);
	}
	/* public instance */ function listen () {
	$buffer = $this->buffer;
	$this->buffer = "";

	  while ($r = fgets ($this->pipes[1], 1024)) {
				$buffer .= $r;
		}


		
	return $buffer;
	}
	/* public instance */ function getError () {
	$buffer = "";
	while ($r = fgets ($this->pipes[2], 255)) {
	$buffer .= $r;
	}
	return $buffer;
	}
}
class Thread {
	var $pref ; // process reference
	var $pipes; // stdio
	var $buffer; // output buffer
	/* private */ function Thread() {
	$this->pref = 0;
	$this->buffer = "";
	$this->pipes = (array)NULL;
	}
	/* public static */ function Create ($file) {
	$t = new Thread;
	$descriptor = array (0 => array ("pipe", "r"), 1 => array ("pipe", "w"), 2 => array ("pipe", "w"));
	$t->pref = proc_open ($file, $descriptor, $t->pipes);
	stream_set_blocking ($t->pipes[1], 0);
	return $t;
	}
	/* public instance */ function isActive () {
	$this->buffer .= $this->listen();
	//echo $this->buffer;
	$f = stream_get_meta_data ($this->pipes[1]);
	return !$f["eof"];
	}
	/* public instance */ function close () {
	$r = proc_close ($this->pref);
	$this->pref = NULL;
	return $r;
	}
	/* public instance */ function tell ($thought) {
	fwrite ($this->pipes[0], $thought);
	}
	/* public instance */ function listen () {
	$buffer = $this->buffer;
	$this->buffer = "";

	
	$r = fgets ($this->pipes[1]);
	$buffer.=$r;
	return $buffer;
	}
	/* public instance */ function getError () {
	$buffer = "";
	while ($r = fgets ($this->pipes[2], 1024)) {
	$buffer .= $r;
	}
	return $buffer;
	}
}

  function my_ip($dest='64.0.0.0', $port=80)
  {
	//david at eder dot us http://uk.php.net/manual/en/function.socket-create.php
    $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    socket_connect($socket, $dest, $port);
    socket_getsockname($socket, $addr, $port);
    socket_close($socket);
    return $addr;
  }
  
  function RegisterWidget($Title,$URL,$URLConfig){
	global $config;
	$sql="select WidgetID from xAP_Widget_Library where Module='".$config["modulefolder"]."' and Title='".dbesc($Title)."'";
	$result=dbquery($sql);
	if (count($result)==0){
		$sql="insert into xAP_Widget_Library (Module,Title,URL,URLConfig) VALUES 
		('".$config["modulefolder"]."',
		'".$Title."',
		'".$URL."',
		'".$URLConfig."')";
		dbexec($sql);
	}else{
		$sql="update xAP_Widget_Library SET
		Module='".$config["modulefolder"]."',
		Title='".$Title."',
		URL='".$URL."',
		URLConfig='".$URLConfig."'
		where WidgetID=".$result[0]["WidgetID"];
		dbexec($sql);
	}
	
  
  }
  

?>